﻿using System;
using System.Data;
using System.Collections.Generic;

using Catering_Model;
using System.Text;
namespace Catering_Rules
{
    /// <summary>
    /// AttendanceRecord
    /// </summary>
    public partial class AttendanceRecord
    {
        private readonly Catering_Operation.AttendanceRecord dal = new Catering_Operation.AttendanceRecord();
        public AttendanceRecord()
        { }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteListBywhere(string where)
        {
            return dal.DeleteListBywhere(where);
        }
        /// <summary>
        /// 月考勤查询
        /// </summary>
        /// <param name="AREAID">作业区ID</param>
        /// <param name="CLASSID">班组ID</param>
        /// <param name="DTIME">日期</param>
        /// <param name="NAME">名字</param>
        /// <param name="PNAME">岗位</param>
        /// <returns></returns>
        public DataTable GetKaoQinForMonth(string pid, string AREAID, string CLASSID, DateTime DTIME, string NAME, string PNAME)
        {
            StringBuilder where = new StringBuilder();
            if (!string.IsNullOrEmpty(AREAID) && !AREAID.Equals("-10"))
            {
                where.Append(string.Format(" and AreaID = {0}", AREAID));
            }
            else
            {
                string strarealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
                if (!string.IsNullOrEmpty(strarealist))
                {
                    where.Append(string.Format(" AND AreaID IN ({0}) ", strarealist));
                }
            }
            if (!string.IsNullOrEmpty(CLASSID) && !CLASSID.Equals("-10"))
            {
                where.Append(string.Format(" and ClassID = {0}", CLASSID));
            }
            if (DTIME != null && !DTIME.Year.Equals("1949"))
            {
                where.Append(string.Format(" and Year(Date) = {0}", DTIME.Year));
                if (!DTIME.Second.Equals(11))
                {
                    where.Append(string.Format(" and Month(Date) = {0}", DTIME.Month));
                }
            }
            if (!string.IsNullOrEmpty(NAME))
            {
                where.Append(string.Format(" and Name like '%{0}%'", NAME));
            }
            if (!string.IsNullOrEmpty(PNAME))
            {
                where.Append(string.Format(" and PName like '%{0}%'", PNAME));
            }
            DataTable dt = dal.GetKaoQinForMonth(where.ToString());
            DataTable tempdt = new DataTable();
            tempdt.Columns.Add("tempid", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("Name", typeof(string));//增加字段ClassID
            tempdt.Columns.Add("PName", typeof(string));//增加字段ClassID
            tempdt.Columns.Add("D1", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D2", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D3", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D4", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D5", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D6", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D7", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D8", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D9", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D10", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D11", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D12", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D13", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D14", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D15", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D16", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D17", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D18", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D19", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D20", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D21", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D22", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D23", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D24", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D25", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D26", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D27", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D28", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D29", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D30", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D31", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("CountKaoQin", typeof(int));//增加字段ClassID
            Dictionary<string, int> tempdic = new Dictionary<string, int>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //dt.Rows[i]["tempid"] = i + 1;
                string pName = dt.Rows[i]["PName"].ToString();
                string name = dt.Rows[i]["Name"].ToString();
                for (int j = 1; j <= 31; j++)
                {
                    int tempnum = Convert.ToInt32(dt.Rows[i]["D" + j]);
                    if (tempnum > 0)
                    {
                        if (tempdic.ContainsKey(pName + "|" + name))
                        {
                            tempdt.Rows[tempdic[pName + "|" + name]]["D" + j] = tempnum;
                        }
                        else
                        {
                            tempdic.Add(pName + "|" + name, tempdic.Count);
                            DataRow dr = tempdt.NewRow();
                            dr["tempid"] = tempdic[pName + "|" + name] + 1;
                            dr["PName"] = pName;
                            dr["Name"] = name;
                            dr["D" + j] = tempnum;
                            tempdt.Rows.Add(dr);
                        }
                    }
                }
            }
            for (int i = 0; i < tempdt.Rows.Count; i++)
            {
                decimal count = 0;
                for (int ii = 1; ii <= 31; ii++)
                {
                    count += FormatDecimal(tempdt.Rows[i]["D" + ii]);
                }
                tempdt.Rows[i]["CountKaoQin"] = count;
            }
            dt.Dispose();
            return tempdt;
        }

        public DataTable GetKaoQin(string pid, string AREAID, string CLASSID, DateTime DTIME, string NAME, string PNAME)
        {
            StringBuilder where = new StringBuilder();
            if (!string.IsNullOrEmpty(AREAID) && !AREAID.Equals("-10"))
            {
                where.Append(string.Format(" and AreaID = {0}", AREAID));
            }
            else
            {
                string strarealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
                if (!string.IsNullOrEmpty(strarealist))
                {
                    where.Append(string.Format(" AND AreaID IN ({0}) ", strarealist));
                }
            }
            if (!string.IsNullOrEmpty(CLASSID) && !CLASSID.Equals("-10"))
            {
                where.Append(string.Format(" and ClassID = {0}", CLASSID));
            }
            if (DTIME != null && !DTIME.Year.Equals("1949"))
            {
                where.Append(string.Format(" and Year(Date) = {0}", DTIME.Year));
            }
            if (!string.IsNullOrEmpty(NAME))
            {
                where.Append(string.Format(" and Name like '%{0}%'", NAME));
            }
            if (!string.IsNullOrEmpty(PNAME))
            {
                where.Append(string.Format(" and PName like '%{0}%'", PNAME));
            }
            DataTable dt = dal.GetKaoQin(where.ToString());
            DataTable tempdt = new DataTable();
            tempdt.Columns.Add("tempid", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("Name", typeof(string));//增加字段ClassID
            tempdt.Columns.Add("PName", typeof(string));//增加字段ClassID
            tempdt.Columns.Add("D1", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D2", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D3", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D4", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D5", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D6", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D7", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D8", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D9", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D10", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D11", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("D12", typeof(int));//增加字段ClassID
            tempdt.Columns.Add("CountKaoQin", typeof(int));//增加字段ClassID
            Dictionary<string, int> tempdic = new Dictionary<string, int>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //dt.Rows[i]["tempid"] = i + 1;
                string pName = dt.Rows[i]["PName"].ToString();
                string name = dt.Rows[i]["Name"].ToString();
                for (int j = 1; j <= 12; j++)
                {
                    int tempnum = Convert.ToInt32(dt.Rows[i]["D" + j]);
                    if (tempnum > 0)
                    {
                        if (tempdic.ContainsKey(pName + "|" + name))
                        {
                            int oldnum = 0;
                            if (!(tempdt.Rows[tempdic[pName + "|" + name]]["D" + j] is DBNull)) oldnum = Convert.ToInt32(tempdt.Rows[tempdic[pName + "|" + name]]["D" + j]);
                            tempdt.Rows[tempdic[pName + "|" + name]]["D" + j] = oldnum + tempnum;
                        }
                        else
                        {
                            tempdic.Add(pName + "|" + name, tempdic.Count);
                            DataRow dr = tempdt.NewRow();
                            dr["tempid"] = tempdic[pName + "|" + name] + 1;
                            dr["PName"] = pName;
                            dr["Name"] = name;
                            dr["D" + j] = tempnum;
                            tempdt.Rows.Add(dr);
                        }
                    }
                }
            }

            for (int i = 0; i < tempdt.Rows.Count; i++)
            {
                decimal count = 0;
                for (int ii = 1; ii <= 12; ii++)
                {
                    count += FormatDecimal(tempdt.Rows[i]["D" + ii]);
                }
                tempdt.Rows[i]["CountKaoQin"] = count;
            }
            dt.Dispose();
            return tempdt;
        }

        /// <summary>
        /// 转换类型
        /// </summary>
        /// <param name="dc"></param>
        /// <returns></returns>
        private decimal FormatDecimal(object obj)
        {
            decimal temp;
            if (obj != null && !string.IsNullOrEmpty(obj.ToString()))
            {
                temp = Convert.ToDecimal(obj.ToString());
            }
            else
            {
                temp = 0;
            }
            return temp;
        }

        /// <summary>
        /// 获取用户在岗状态
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public int GetPersonnelStateByPersonnelID(int pid, DateTime date)
        {
            return dal.GetPersonnelStateByPersonnelID(pid, date);
        }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return dal.Exists(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Catering_Model.AttendanceRecord model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Catering_Model.AttendanceRecord model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            return dal.Delete(id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            return dal.DeleteList(idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Catering_Model.AttendanceRecord GetModel(int id)
        {

            return dal.GetModel(id);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public Catering_Model.AttendanceRecord GetModelByCache(int id)
        {

            string CacheKey = "AttendanceRecordModel-" + id;
            object objModel = Catering_DBOpertion.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(id);
                    if (objModel != null)
                    {
                        int ModelCache = Catering_DBOpertion.ConfigHelper.GetConfigInt("ModelCache");
                        Catering_DBOpertion.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (Catering_Model.AttendanceRecord)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Catering_Model.AttendanceRecord> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Catering_Model.AttendanceRecord> DataTableToList(DataTable dt)
        {
            List<Catering_Model.AttendanceRecord> modelList = new List<Catering_Model.AttendanceRecord>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Catering_Model.AttendanceRecord model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Catering_Model.AttendanceRecord();
                    if (dt.Rows[n]["id"].ToString() != "")
                    {
                        model.id = int.Parse(dt.Rows[n]["id"].ToString());
                    }
                    if (dt.Rows[n]["PID"].ToString() != "")
                    {
                        model.PID = int.Parse(dt.Rows[n]["PID"].ToString());
                    }
                    if (dt.Rows[n]["ClassID"].ToString() != "")
                    {
                        model.ClassID = int.Parse(dt.Rows[n]["ClassID"].ToString());
                    }
                    model.PName = dt.Rows[n]["PName"].ToString();
                    if (dt.Rows[n]["Date"].ToString() != "")
                    {
                        model.Date = DateTime.Parse(dt.Rows[n]["Date"].ToString());
                    }
                    model.State = dt.Rows[n]["State"].ToString();
                    model.temp1 = dt.Rows[n]["temp1"].ToString();
                    model.temp2 = dt.Rows[n]["temp2"].ToString();
                    if (dt.Rows[n]["RID"].ToString() != "")
                    {
                        model.RID = int.Parse(dt.Rows[n]["RID"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  Method
    }
}

